package com.htl.线程.sleep_wait;

import java.util.concurrent.TimeUnit;

public class Sleep_wait {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.test(i);
            }
        }).start();
    }
}

class Data{
    public synchronized void test(int i){
        if (i == 5) {
            try {
                // wait 让线程对象休眠，没有阻塞时间；如果不加处理，线程会永远阻塞下去。
                this.wait(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(i + "-----------");
    }
}
